package com.ph.elec.template;

import org.apache.click.Page;
import org.apache.click.control.PageLink;
import org.apache.click.extras.control.ExternalLink;
import org.apache.click.util.ClickUtils;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.ph.elec.page.register.PreStartPage;
import com.ph.elec.page.register.StartPage;
import com.ph.elec.panel.RightSearchPanel;

public class BasePage extends Page {
	
	protected static final String CSS_IMPORT = 
        	"<link type='text/css' rel='stylesheet' href='/static/default.css' />\n"; 
	
	public String title = "CHANGE ME!!!";
	public String keywords = "";
	public String description = "";

	public String root_url = "/page/index.htm";
	
	public RightSearchPanel rightPanel;
	
	public User user;
	public ExternalLink loginLink;
	public ExternalLink logoutLink;
	public PageLink register;
	
	public BasePage() {
		rightPanel = new RightSearchPanel();
		addControl(rightPanel);
		
		// Attempt to retrieve the user
		UserService userService = UserServiceFactory.getUserService();
        user = userService.getCurrentUser();
        if (user == null) {
        	loginLink = new ExternalLink("login_link", 
        			userService.createLoginURL(getContext().getRequest().getRequestURI()));
        	loginLink.setLabel("Login!");
        	loginLink.setTitle("Login using a Google account " +
        			"and get access to more features of the portal site!");
        	register = new PageLink("register", "Register as a Volunteer!", PreStartPage.class);
        } else {
        	logoutLink = new ExternalLink("logout_link", 
        			userService.createLogoutURL(getContext().getResourcePath().replaceFirst("/page.*", root_url)));
        	logoutLink.setLabel("Logout");
        	logoutLink.setTitle("Logout of the present Google account.");
        	register = new PageLink("register", "Register as a Volunteer!", StartPage.class);
        }
	}
	
	@Override
	public String getTemplate() {
		return "/tmpl/base.htm";
	}
	
	@Override
	public String getHtmlImports() {
		return ClickUtils.createHtmlImport(CSS_IMPORT, getContext());
	}

}
